package com.sharding.jdbc.mybatis.algorithm;

import io.shardingsphere.core.api.algorithm.sharding.ListShardingValue;
import io.shardingsphere.core.api.algorithm.sharding.ShardingValue;
import io.shardingsphere.core.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Value3TableAlgorithm implements ComplexKeysShardingAlgorithm {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, Collection<ShardingValue> shardingValues) {
//        System.out.println("availableTargetNames="+availableTargetNames);
//        System.out.println("shardingValues="+shardingValues);

        ListShardingValue[] values = new ListShardingValue[shardingValues.size()];
        shardingValues.toArray(values);

        List<String> tables = new LinkedList<>();

        for (ListShardingValue value : values) {
            String val = String.valueOf(Long.parseLong(String.valueOf(value.getValues().toArray()[0])) % 4);

            tables.add(value.getLogicTableName() + val);
        }

        return tables;
    }
}
